﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;

public partial class Default2 : System.Web.UI.Page
{
    string conexicon = ConfigurationManager.ConnectionStrings["conexion"].ConnectionString;
    DataTable Tabla;

    protected void Page_Load(object sender, EventArgs e)
    {
        Excel.Visible = false;
        Pdf.Visible = false;
        Word.Visible = false;
        if (!Page.IsPostBack)
        {

            string query = "SELECT Codigo, Linea + ' ' + Tubo as Lineatubo FROM OPTIMAS WHERE empaque=1";
            SqlDataAdapter da = new SqlDataAdapter(query, conexicon);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ComboLinea.DataSource = dt;
            ComboLinea.DataTextField = "LineaTubo";
            ComboLinea.DataValueField = "Codigo";
            DataBind();

        }
        else
        {
            Tabla = (DataTable)Session["Data"];
            RadGrid1.DataSource = Tabla;
            DataBind();
            Excel.Visible = true;
            Pdf.Visible = true;
            Word.Visible = true;

        }
     

    }

    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {
       
        string QueryReporte = "exec reporte'" + ComboTiempo.SelectedValue + "','" + ComboLinea.SelectedValue + "','"+RadFecha.SelectedDate+"'";
        Tabla = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(QueryReporte, conexicon);
        
        da.Fill(Tabla);
        RadGrid1.DataSource = Tabla;
        DataBind();
        Session["Data"] = Tabla;

        if (Tabla.Rows.Count > 0)
        {
            Excel.Visible = true;
            Pdf.Visible = true;
            Word.Visible = true;

            //Se genera el total de las bolsas:
            string contador = Tabla.Rows.Count.ToString();
            LbTotal.Text = contador;

            
            ////Se genera el promedio de los registos obtenidos de la tabalas peso.
            
            //decimal cantidad = Tabla.Rows.Count;
            //decimal total = 0;
            

            //foreach (DataRow fila in Tabla.Rows)
            //{
            //    total += Convert.ToDecimal(fila["peso"].ToString());


            //}

            //decimal promedio = (total / cantidad);
            //decimal PromedioRedondo = Math.Round(promedio, 1);
            //LbPromedio.Text = PromedioRedondo.ToString();

            //////////////////Nuevo Codigo vairanza////////////////////////



            //Object[] Datos = new Object[Tabla.Rows.Count];

            //for (int i = 0; i < Tabla.Rows.Count; i++)
            //{

            //    Datos[i] = Math.Pow(Convert.ToDouble(Tabla.Rows[i]["peso"]) - promedio, 2);

            //    //Datos[i] = Convert.ToDecimal (Tabla.Rows[i]["peso"]) - (promedio);
            //}



            // Desv Estandart
            List<double> Datos = new List<Double>();


           for (int i = 0; i < Tabla.Rows.Count; i++)
           {
               Datos.Add(Convert.ToDouble(Tabla.Rows[i]["peso"]));

               //Datos[i] = Math.Pow(Convert.ToDouble(Tabla.Rows[i]["peso"]) - promedio, 2);

               //Datos[i] = Convert.ToDecimal (Tabla.Rows[i]["peso"]) - (promedio);
           }

           Double promedio = Datos.Average();
           double promedioRound = Math.Round(promedio, 2);
            LbPromedio.Text = promedioRound.ToString();

           Double sumas = Datos.Select(val => Math.Pow(val - promedio, 2)).Sum();

           Double Desviacion = Math.Sqrt(sumas / (Datos.Count-1));
           double DesviacionRound = Math.Round(Desviacion, 2);

           LbDesv.Text = DesviacionRound.ToString();

            
            //Target
            DataRow row = Tabla.Rows[0];
            string target;
            target = Convert.ToString(row["Target"]);
            LbTarget.Text = target;

            //Op   
            Double  op;

            op = promedio - Convert.ToDouble(target) / Convert.ToDouble(target);
            double opRound = Math.Round(op, 2);
            LbOp.Text = opRound.ToString();

            //TZ
            decimal Tz;
            Tz =  Convert.ToDecimal(promedio) - Convert.ToDecimal( target) /Convert.ToDecimal (Desviacion);
            decimal TzRound = Math.Round(Tz, 2);
            LbTz.Text = TzRound.ToString();

            // % Packing
            decimal OnPacking;
         OnPacking= (Convert.ToDecimal( promedio) - Convert.ToDecimal( target)) / (Convert.ToDecimal( target))*100;
         decimal OnPackingRound = Math.Round(OnPacking, 2);
      
         LbOnPacking.Text = OnPackingRound.ToString();

            {
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.MasterTableView.ExportToPdf();
    }
    protected void Excel_Click(object sender, ImageClickEventArgs e)
    {
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.MasterTableView.ExportToExcel();
    }
    protected void Pdf_Click(object sender, ImageClickEventArgs e)
    {
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.MasterTableView.ExportToPdf();

    }
    protected void Word_Click(object sender, ImageClickEventArgs e)
    {
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.MasterTableView.ExportToWord();
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {

    }
}